package com.itheima.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.log.LogAnno;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;


    @LogAnno("预约设置")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    @RequestMapping("/add")
    public Result add(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.add(orderSetting);
            return Result.success("预约设置成功");
        } catch (Exception e) {
            return Result.error("预约设置失败");
        }
    }
    @LogAnno("查询预约")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    @RequestMapping("/findPage")
    public Result findPage(String date) {
        try {
            List<OrderSetting> orderSettings = orderSettingService.findPage(date);
            return Result.success("查询预约成功", orderSettings);
        } catch (Exception e) {
            return Result.error("查询预约失败");
        }
    }
    @LogAnno("预约批量上传")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] string : strings) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(DateUtil.parse(string[0], "yyyy/MM/dd"));
                orderSetting.setNumber(Integer.parseInt(string[1]));
                orderSettings.add(orderSetting);
            }
            orderSettingService.upload(orderSettings);
            return Result.success("预约批量上传成功");
        } catch (Exception e) {
            return Result.error("预约批量上传失败");
        }
    }


}
